//Function Maintenance
var zTree1, rMenu;
var zNodeTrees;
var setting = {
		view: {
			dblClickExpand: false
			
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		treeNodeKey: "id", 
		treeNodeParentKey: "pId",
		callback: {
			onClick: onClick,
			onRightClick: OnRightClick
		}
	};

	function onClick(e,treeId, treeNode) {
		var zTree1 = $.fn.zTree.getZTreeObj("treeMaintenance");
		zTree1.expandNode(treeNode);
		if(treeNode &&treeNode.level==1)//group
		{
			zTree1.selectNode(treeNode);
			onCheckGroup(treeNode);
		}else if(treeNode && treeNode.level>1 && treeNode.isParent)//if group have group
		{
			zTree1.selectNode(treeNode);
			onCheckGroup(treeNode);
		}else if(treeNode && treeNode.level>1 && !treeNode.isParent)//the last level
		{
			zTree1.selectNode(treeNode);
			onCheckFunction(treeNode);
		}
		
	}
	//if check group show group info and clean function info 
	function onCheckGroup(treeNode)
	{
		var groupID=treeNode.id;
		if(groupID!=0)
			{
				FunctionDWRService.findJsonFunctionGroup(groupID,function(data){callbackfindgroup(data);});
			}
	}
	var callbackfindgroup=function callbackresultfindgroup(result)
	{
		var arg=eval(result);
		document.getElementById("groupEdit:txteditgroupname").value=arg.functiongroupname;
		document.getElementById("groupEdit:txteditgroupdescription").value=arg.functiongroupdesc;
		document.getElementById("groupEdit:txteditgroupparentid").value=arg.parentgroupid;
		document.getElementById("groupEdit:txteditgroupgroupSequence").value=arg.seq;
		document.getElementById("groupEdit:txteditgroupurl").value=arg.url;
		document.getElementById("groupEdit:txteditgrouptitle").value=arg.title;
		document.getElementById("groupEdit:txteditgrouproles").value=arg.roles;
		document.getElementById("groupEdit:txteditgroupid").value=arg.id;
		document.getElementById("groupEdit:rdeditgroupenable").checked=arg.enabled==1?true:false;
		document.getElementById("functionEdit:txteditfunctiongroupid").value="";
		document.getElementById("functionEdit:txteditfunctionid").value="";
		document.getElementById("functionEdit:txteditfunctionname").value="";
		document.getElementById("functionEdit:txteditfunctiondescription").value="";
		document.getElementById("functionEdit:txteditfunctionsequence").value="";		
		document.getElementById("functionEdit:rdeditfunctionenable").checked=false;
		document.getElementById("functionEdit:btFunctionbutton").disabled=true;
	};
	function onCheckFunction(treeNode)
	{
		var f_functionID=treeNode.id;
		var functionID=f_functionID.substring(2,f_functionID.length);
		FunctionDWRService.findJsonFunctionList(functionID,function(data){callbackfindfunction(data);});
			
	}
	var callbackfindfunction=function callbackresultfindfunction(result)
	{
		var arg=eval(result);
		var arg1=eval(arg.functiongroup);
		document.getElementById("groupEdit:txteditgroupname").value=arg1.functiongroupname;
		document.getElementById("groupEdit:txteditgroupdescription").value=arg1.functiongroupdesc;
		document.getElementById("groupEdit:txteditgroupparentid").value=arg1.parentgroupid;
		document.getElementById("groupEdit:txteditgroupgroupSequence").value=arg1.seq;
		document.getElementById("groupEdit:txteditgroupurl").value=arg1.url;
		document.getElementById("groupEdit:txteditgrouptitle").value=arg1.title;
		document.getElementById("groupEdit:txteditgrouproles").value=arg1.roles;
		document.getElementById("groupEdit:txteditgroupid").value=arg1.id;
		document.getElementById("groupEdit:rdeditgroupenable").checked=arg1.enabled==1?true:false;
		
		document.getElementById("functionEdit:txteditfunctiongroupid").value=arg.functiongroupid;
		document.getElementById("functionEdit:txteditfunctionid").value=arg.id;
		document.getElementById("functionEdit:txteditfunctionname").value=arg.functionname;
		document.getElementById("functionEdit:txteditfunctiondescription").value=arg.functiondesc;
		document.getElementById("functionEdit:txteditfunctionsequence").value=arg.seq;		
		document.getElementById("functionEdit:rdeditfunctionenable").checked=arg.enabled==1?true:false;
		document.getElementById("functionEdit:btFunctionbutton").disabled=false;
	};
	
	
	function OnRightClick(event, treeId, treeNode) {		
		if(treeNode && treeNode.level==0)//home
		{
			zTree1.selectNode(treeNode);
			showRMenu("home", event.clientX, event.clientY);
		}else if(treeNode &&treeNode.level==1)//group
		{
			zTree1.selectNode(treeNode);
			showRMenu("group", event.clientX, event.clientY);
		}else if(treeNode && treeNode.level>1 && treeNode.isParent)//if group have group
		{
			zTree1.selectNode(treeNode);
			showRMenu("depgroup", event.clientX, event.clientY);
		}else if(treeNode && treeNode.level>1 && !treeNode.isParent)//the last level
		{
			zTree1.selectNode(treeNode);
			showRMenu("child", event.clientX, event.clientY);
		}
	}

	function showRMenu(type, x, y) {
		$("#rMenu ul").show();
		if (type=="home") {
			$("#m_addgroup").show();
			$("#m_addfunction").hide();
			$("#m_delgroup").hide();
			$("#m_delfunction").hide();
		} else if(type=="group"||type=="depgroup"){
			$("#m_addgroup").show();
			$("#m_addfunction").show();
			$("#m_delgroup").show();
			$("#m_delfunction").show();
		}else if(type=="child"){
			$("#m_addgroup").show();
			$("#m_addfunction").show();
			$("#m_delgroup").hide();
			$("#m_delfunction").show();
		}
		rMenu.css({"top":y+"px", "left":x+"px" ,"visibility":"visible"});
		$("body").bind("mousedown", onBodyMouseDown);
	}
	function hideRMenu() {
		if (rMenu) rMenu.css({"visibility": "hidden"});
		$("body").unbind("mousedown", onBodyMouseDown);
	}
	function onBodyMouseDown(event){
		if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
			rMenu.css({"visibility" : "hidden"});
		}
	}
	var addCount = 1;
	function addTreeNode() {
		hideRMenu();
		showLayer('1');
		FunctionDWRService.searchTreeJsonFunction(function(data){callbackaddfunction(data);});
		//zTree1.addNodes(zTree1.getSelectedNodes()[0], [{ name:"增加" + (addCount++)}]);
	}
	
	
	
	
	function removeTreeNode() {
		hideRMenu();
		var nodes = zTree.getSelectedNodes();
		if (nodes && nodes.length>0) {
			if (nodes[0].childs && nodes[0].childs.length > 0) {
				var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
				if (confirm(msg)==true){
					zTree1.removeNode(nodes[0]);
				}
			} else {
				zTree1.removeNode(nodes[0]);
			}
		}
	}
	
//Function Assignment	
	var setting1 = {
			view: {
				selectedMulti: false
			},
			check: {
				enable: true
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				beforeCheck: beforeCheck,
				onCheck: onCheck
			},
			treeNodeKey: "id", 
			treeNodeParentKey: "pId"
		};

		
		
		var code, log, className = "dark";
		function beforeCheck(treeId, treeNode) {
			className = (className === "dark" ? "":"dark");
			showLog("[ "+getTime()+" beforeCheck ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name );
			return (treeNode.doCheck !== false);
		}
		function onCheck(e, treeId, treeNode) {
			showLog("[ "+getTime()+" onCheck ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name );
		}		
		function showLog(str) {
			if (!log) log = $("#log");
			log.append("<li class='"+className+"'>"+str+"</li>");
			if(log.children("li").length > 6) {
				log.get(0).removeChild(log.children("li")[0]);
			}
		}
		function getTime() {
			var now= new Date(),
			h=now.getHours(),
			m=now.getMinutes(),
			s=now.getSeconds(),
			ms=now.getMilliseconds();
			return (h+":"+m+":"+s+ " " +ms);
		}

		function checkNode(e) {
			var zTree = $.fn.zTree.getZTreeObj("treeAssignment"),
			type = e.data.type,
			nodes = zTree.getSelectedNodes();
			if (type.indexOf("All")<0 && nodes.length == 0) {
				alert("请先选择一个节点");
			}

			if (type == "checkAllTrue") {
				zTree.checkAllNodes(true);
			} else if (type == "checkAllFalse") {
				zTree.checkAllNodes(false);
			} else {
				for (var i=0, l=nodes.length; i<l; i++) {
					if (type == "checkTrue") {
						zTree.checkNode(nodes[i], true);
					} else if (type == "checkFalse") {
						zTree.checkNode(nodes[i], false);
					}else if (type == "checkTruePS") {
						zTree.checkNode(nodes[i], true, true);
					} else if (type == "checkFalsePS") {
						zTree.checkNode(nodes[i], false, true);
					}
				}
			}
		}

		$(document).ready(function(){
			$('#primary').tabs();
			searchTreeJsonFunction();			
		});
		function searchTreeJsonFunction()
		{
			FunctionDWRService.searchTreeJsonFunction(function(data){callbacksearchtreefunction(data);});
		}
		var callbacksearchtreefunction=function callbackresultsearchtreefunction(result)
		{
			zNodeTrees=eval(result);			
			$.fn.zTree.init($("#treeAssignment"), setting1, zNodeTrees);
			$("#checkTrue").bind("click", {type:"checkTrue"}, checkNode);
			$("#checkFalse").bind("click", {type:"checkFalse"}, checkNode);
			$("#checkTruePS").bind("click", {type:"checkTruePS"}, checkNode);
			$("#checkFalsePS").bind("click", {type:"checkFalsePS"}, checkNode);
			$("#checkAllTrue").bind("click", {type:"checkAllTrue"}, checkNode);
			$("#checkAllFalse").bind("click", {type:"checkAllFalse"}, checkNode);
			
			$.fn.zTree.init($("#treeMaintenance"), setting, zNodeTrees);
			zTree1 = $.fn.zTree.getZTreeObj("treeMaintenance");
			rMenu = $("#rMenu");
		};
